import {
  request,
  BasicUrl
} from "../../api/config";

// pages/order/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    BasicUrl: '',
    tabsKey:1,
    // 当前订单
    cuurOrder: [],
    // 历史订单
    historyOrder: [],
    // 外卖订单
    deliveryOrders:[],
    tabs1: 'currOrder',
    // 评价
    showeValuate:false,
    // 评分
    Valuate:0,
    // 评价意见
    evaluationOpinions:'',
    // 评价订单id
    evaluate_order_id:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
   onLoad(options) {
    this.setData({
      BasicUrl: BasicUrl
    })
  },
  // 获取当前订单
 async  getCurrOrder() {
  let user_id = wx.getStorageSync('user_info') ? JSON.parse(wx.getStorageSync('user_info')).user_id : {}
    let query = {
      user_id,
      order_status: 1,
      order_type: 1
    }
    wx.showLoading({
      title: '加载中...',
    })
   await  request('/api/order/list', 'post', query, true).then(({
      data
    }) => {
      console.log(data);
      data.data.forEach(item => {
        item.goods_detail = JSON.parse(item.goods_detail.replace(/\r\n+/g, ''))
        item.store_info = JSON.parse(item.store_info.replace(/\r\n+/g, ''))
      })
      if (data.code == 200) {
        this.setData({
          cuurOrder: data.data,
          tabsKey:this.data.tabsKey++
        })
      }
    })
    wx.hideLoading()
  },
  // 获取历史订单
  async getHistoryOrder(){
    let user_id = wx.getStorageSync('user_info') ? JSON.parse(wx.getStorageSync('user_info')).user_id : {}
    let query = {
      user_id,
      order_status: 2,
      order_type: 1
    }
    wx.showLoading({
      title: '加载中...',
    })
    await request('/api/order/list', 'post', query, true).then(({
      data
    }) => {
      console.log(data);
      data.data.forEach(item => {
        item.goods_detail = JSON.parse(item.goods_detail.replace(/\r\n+/g, ''))
        item.store_info = JSON.parse(item.store_info.replace(/\r\n+/g, ''))
      })
      if (data.code == 200) {
        this.setData({
          historyOrder: data.data
        })
      }
    })
    wx.hideLoading()
  },
  // 获取外卖订单
  async deliveryOrders(){
    let user_id = wx.getStorageSync('user_info') ? JSON.parse(wx.getStorageSync('user_info')).user_id : {}
    let query = {
      user_id,
      order_type: 2
    }
    wx.showLoading({
      title: '加载中...',
    })
    await request('/api/order/list', 'post', query, true).then(({
      data
    }) => {
      console.log(data);
      data.data.forEach(item => {
        item.goods_detail = JSON.parse(item.goods_detail.replace(/\r\n+/g, ''))
        item.store_info = JSON.parse(item.store_info.replace(/\r\n+/g, ''))
      })
      if (data.code == 200) {
        this.setData({
          deliveryOrders: data.data
        })
      }
    })
    wx.hideLoading()
  },
  CancellationOfOrder(e) {
    let order_id = e.currentTarget.dataset.id
    let user_id = JSON.parse(wx.getStorageSync('user_info')).user_id 
        wx.showModal({
          title: '提示',
          content: '确认要取消该订单?',
          complete: (res) => {
             // 点击了取消
            if (res.cancel) {
              
            }
            // 点了确认
            if (res.confirm) {
              request('/api/order/cancel_order', 'post', {
                order_id,
                user_id
              }, true).then(({
                data
              }) => {
                console.log(data);
                if (data.code == 200) {
                  wx.showToast({
                    title: '取消成功',
                    icon: 'success'
                  })
                 this.getCurrOrder()
                 this.getHistoryOrder()
                 this.deliveryOrders()
                } else {
                  wx.showToast({
                    title: '取消失败',
                    icon: 'error'
                  })
                }
              })
            }
          }
        })
  },
  // 切换店内订单
  ChangeinStoreOrder(event){
    console.log('tabs1',this.data.tabs1);
    let {index} = event.detail
    if(index == 0){
      this.getCurrOrder()
    }else{
      this.getHistoryOrder()
    }
  },
  // 切换顶部订单类型
  changeOrderType(event) {
    console.log(event.detail);
    let {index} = event.detail
    if(index == 1){
      this.deliveryOrders()
    }
  },
  // 顶部home图标
  goHome() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 去点单
  toMeal() {
    wx.switchTab({
      url: '/pages/meal/index'
    })
  },

  // 支付
  toBalance(e) {
    console.log(e);
    let order_id = e.currentTarget.dataset.id
    let user_id = JSON.parse(wx.getStorageSync('user_info')).user_id
    wx.showModal({
      title: '模拟支付',
      content: '确认支付？',
      complete: (res) => {
        // 点击了取消
        if (res.cancel) {
          wx.switchTab({
            url: '/pages/order/index',
          })
          console.log('cancel', res.cancel);
        }
        // 点击了确认
        if (res.confirm) {
          request('/api/order/pay_order', 'post', {
            order_id,
            user_id
          }, true).then(({
            data
          }) => {
            console.log(data);
            if (data.code == 200) {
              wx.showToast({
                title: '支付成功',
                icon: 'success'
              })
              this.getCurrOrder()
              this.deliveryOrders()
              this.getHistoryOrder()
            } else {
              wx.showToast({
                title: '支付失败',
                icon: 'error'
              })
            }
          })

        }
      }
    })
  },
  // 去评价
  toEvaluate(e){
    this.setData({
      evaluate_order_id:e.currentTarget.dataset.id
    })
    this.setData({
      showeValuate:true,
      evaluationOpinions:'',
      Valuate:0
    })
  },
  // 关闭评价弹窗
  CloseValuate(){
    this.setData({
      showeValuate:false
    })
  },
  // 提交评价
  submitValuate(e){
    if(this.data.Valuate == 0){
      return wx.showToast({
        title: '请完善评价信息',
        icon:'none'
      })
    }
    let user_id = JSON.parse(wx.getStorageSync('user_info')).user_id
    let query = {
      evaluationOpinions:this.data.evaluationOpinions,
      valuate:this.data.Valuate,
      order_id:this.data.evaluate_order_id,
      user_id:user_id
    }
    console.log(query);
    request('/api/order/evaluation_order','post',query,true).then(({data})=>{
      if(data.code == 200){
        wx.showToast({
          title: '评价成功',
        })
        this.getCurrOrder()
        this.deliveryOrders()
        this.getHistoryOrder()
      }else{
        wx.showToast({
          title: '评价失败',
          icon:'error'
        })
      }
    })
    this.setData({
      showeValuate:false,
      evaluate_order_id:''
    })
  },
  // 监听输评价意见
  inputevaluat(e){
    console.log(e.detail);
    this.setData({
      evaluationOpinions:e.detail
    })
  },
  // 监听评分
  ChangeValuate(e){
    console.log(e.detail);
    this.setData({
      Valuate:e.detail
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      tabs1:'currOrder'
    })
    this.getCurrOrder()
    this.deliveryOrders()
    this.getHistoryOrder()
    this.getTabBar().setData({
      active: 2
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})